<template>
  <div>
    <a-alert message="开发者平台" type="info" show-icon style="margin-bottom: 18px">
      <template #description>
        使用开发者平台 文件存储服务，阿里云OSS、腾讯云COS一键切换，开通地址：<a
          href="http://console.21ds.cn/register"
          target="_blank"
          >立即注册</a
        >
      </template>
    </a-alert>
    <a-form
      ref="FormRef"
      :model="FormState"
      :rules="FormRule"
      :labelCol="labelCol"
      :wrapperCol="wrapperCol"
    >
      <a-form-item label="是否开启">
        <a-switch
          v-model:checked="FormState.open_bool"
          checked-children="是"
          un-checked-children="否"
        />
      </a-form-item>
      <a-form-item
        v-show="FormState.open_bool"
        label="开发者key"
        has-feedback
        required
        name="dev_key"
      >
        <a-input v-model:value="FormState.dev_key" placeholder="请填写开发者key" />
      </a-form-item>
      <a-form-item
        v-show="FormState.open_bool"
        label="文件存储配置ID"
        has-feedback
        required
        name="fs_id"
      >
        <a-input v-model:value="FormState.fs_id" placeholder="请填写文件存储配置ID" />
      </a-form-item>
      <a-form-item
        v-show="FormState.open_bool"
        label="文件存储分类ID"
        has-feedback
        required
        name="fc_id"
      >
        <a-input v-model:value="FormState.fc_id" placeholder="请填写文件存储分类ID" />
      </a-form-item>
      <a-form-item
        v-show="FormState.open_bool"
        label="SecretKey"
        has-feedback
        required
        name="secret_key"
      >
        <a-input v-model:value="FormState.secret_key" placeholder="SecretKey 用于签名生成" />
      </a-form-item>
      <a-form-item style="margin-left: 12.5%">
        <a-button type="primary" html-type="submit" :loading="ConfirmLoading" @click="OnSubmit">
          提交
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, toRefs, onMounted, ref, toRaw } from 'vue'
import { Row, Col, Switch } from 'ant-design-vue'
import { requestSuccess, requestFailed } from '@/utils/feedback'
import { getOssSetting, editOssSetting } from '@/api/system'

export default defineComponent({
  name: 'OssSetting',
  components: {
    [Row.name]: Row,
    [Switch.name]: Switch,
    [Col.name]: Col
  },
  setup() {
    const FormRef = ref()
    const state = reactive({
      ConfirmLoading: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      }
    })
    const FormState = reactive({
      dev_key: '',
      fs_id: '',
      open: 0,
      open_bool: false,
      fc_id: '',
      secret_key: ''
    })
    const FormRule = {
      dev_key: [{ required: true, message: '请填写开发者key', trigger: ['blur', 'change'] }],
      fs_id: [{ required: true, message: '请填写文件存储配置ID', trigger: ['blur', 'change'] }],
      fc_id: [{ required: true, message: '请填写文件存储分类ID', trigger: ['blur', 'change'] }],
      secret_key: [{ required: true, message: '请填写SecretKey', trigger: ['blur', 'change'] }]
    }
    onMounted(() => {
      getDataList()
    })

    const tabChange = () => {
      getDataList()
    }

    function getDataList() {
      getOssSetting({}).then((res) => {
        for (let item in res) {
          FormState[item] = res[item]
        }
        FormState.open_bool = FormState.open == 1 ? true : false
      })
    }
    const OnSubmit = () => {
      FormState.open = FormState.open_bool == true ? 1 : 0
      FormRef.value
        .validate()
        .then(() => {
          editOssSetting(toRaw(FormState))
            .then((res) => {
              if (res.code === 200) {
                requestSuccess(res)
              } else {
                requestFailed(res)
              }
            })
            .finally(() => {
              state.ConfirmLoading = false
            })
        })
        .catch(() => {
          return false
        })
    }
    return {
      ...toRefs(state),
      FormRef,
      FormState,
      FormRule,
      tabChange,
      OnSubmit,
      getDataList
    }
  }
})
</script>

<style lang="scss" scoped></style>
